package com.fenger.bus.controller;


import com.fenger.bus.service.ProviderService;
import com.fenger.bus.vo.ProviderVo;
import com.fenger.sys.contast.SysConstast;
import com.fenger.sys.utils.DataGirdView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ProviderController {
    private final static String CUSTOMER_PREFIX = "provider/";

    @Autowired
    ProviderService providerService;

    @ResponseBody
    @RequestMapping(CUSTOMER_PREFIX + "loadAllProvider")
    private DataGirdView loadAllProvider(ProviderVo providerVo) {
        return this.providerService.queryAllProvider(providerVo);
    }

    @ResponseBody
    @RequestMapping(CUSTOMER_PREFIX + "addProvider")
    private DataGirdView addProvider(ProviderVo providerVo) {
        try {
            this.providerService.addProvider(providerVo);
            return new DataGirdView(SysConstast.ADD_SUCCESS_CODE, SysConstast.ADD_SUCCESS_MSG, 1L, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataGirdView(SysConstast.ADD_ERROR_CODE, SysConstast.ADD_ERROR_MSG, 0L, null);
        }
    }

    @ResponseBody
    @RequestMapping(CUSTOMER_PREFIX + "updateProvider")
    private DataGirdView updateProvider(ProviderVo providerVo) {

        try {
            this.providerService.updateProvider(providerVo);
            return new DataGirdView(SysConstast.UPDATE_SUCCESS_CODE, SysConstast.UPDATE_SUCCESS_MSG, 1L, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataGirdView(SysConstast.UPDATE_ERROR_CODE, SysConstast.UPDATE_ERROR_MSG, 0L, null);
        }
    }

    @ResponseBody
    @RequestMapping(CUSTOMER_PREFIX + "deleteProvider")
    private DataGirdView deleteProvider(ProviderVo providerVo) {

        try {
            this.providerService.deleteProvider(providerVo.getId());
            return new DataGirdView(SysConstast.DELETE_SUCCESS_CODE, SysConstast.DELETE_SUCCESS_MSG, 1L, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataGirdView(SysConstast.DELETE_ERROR_CODE, SysConstast.DELETE_ERROR_MSG, 0L, null);
        }
    }


    @ResponseBody
    @RequestMapping(CUSTOMER_PREFIX + "deleteListProvider")
    private DataGirdView deleteListProvider(@RequestParam(value="id[]", required = false) String[] id) {
        System.out.println("--------------------");
        System.out.println(id);
        for (int i = 0; i < id.length; i++) {
            String s = id[i];
            System.out.println(s);
        }
        return new DataGirdView(SysConstast.ERROR_CODE, SysConstast.DELETE_ERROR_MSG, 0L, null);
    }

}
